package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.system.*;
import com.barbeque.mall.bean.system.bo.FirstClass;
import com.barbeque.mall.bean.system.bo.MarketPermissionBO;
import com.barbeque.mall.bean.system.vo.MarketRoleVO;
import com.barbeque.mall.mapper.MarketPermissionMapper;
import com.barbeque.mall.mapper.MarketRoleMapper;
import com.barbeque.mall.mapper.MarketRoleOptionsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 苏东宇
 * @since 2022/07/17 11:19
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Autowired
    MarketRoleOptionsMapper marketRoleOptionsMapper;



    /**
     * 获取所有角色
     * @return java.util.List<com.barbeque.mall.bean.system.MarketRole>
     * @author 苏东宇
     * @since 2022/07/16 14:55
     */
    @Override
    public CommonData<MarketRoleVO> getAdminRoleOptions() {

        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);

        //将查询出来的role转换成vo类
        ArrayList<MarketRoleVO> marketRoleVOS = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            marketRoleVOS.add(new MarketRoleVO(marketRole.getId(), marketRole.getName()));
        }

        PageInfo<MarketRoleVO> marketRolePageInfo = new PageInfo<>(marketRoleVOS);
        return CommonData.data(marketRolePageInfo);
    }

    /**
     * 获取所有角色
 * @param basePageInfo
 * @param name
     * @return com.barbeque.mall.bean.param.CommonData
     * @author 苏东宇
     * @since 2022/07/18 17:11
     */
    @Override
    public CommonData getRoleList(BasePageInfo basePageInfo, String name) {
        PageHelper.startPage(basePageInfo.getPage(),basePageInfo.getLimit());
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (name != null){
            criteria.andNameLike("%"+name+"%");
        }

        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);



        return CommonData.data(marketRolePageInfo);
    }

    /**
     * 新增一个角色
 * @param marketRole
     * @return int
     * @author 苏东宇
     * @since 2022/07/18 17:52
     */
    @Override
    public int createRole(MarketRole marketRole) {
        int insert = marketRoleMapper.insert(marketRole);



        return insert;
    }

    /**
     * 更新角色信息
 * @param marketRole
     * @return int
     * @author 苏东宇
     * @since 2022/07/18 19:55
     */
    @Override
    public int updateRole(MarketRole marketRole) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());

        int i = marketRoleMapper.updateByExample(marketRole,marketRoleExample);

        return i;
    }

    /**
     * 逻辑删除角色，将删除位置为1
 * @param marketRole
     * @return int
     * @author 苏东宇
     * @since 2022/07/18 20:07
     */
    @Override
    public int deleteRole(MarketRole marketRole) {

        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andIdEqualTo(marketRole.getId());

        int i = marketRoleMapper.updateByExample(marketRole, marketRoleExample);
        return i;
    }

    /**
     * 根据角色id查询该角色的所有权限
     * @param roleId
     * @return
     */
    @Override
    public List<String> getPermissionsByRoleId(Integer roleId) {
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);

        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(marketPermissionExample);
        ArrayList<String> assignedPermissions = new ArrayList<>();

        for (MarketPermission marketPermission : marketPermissions) {
            assignedPermissions.add(marketPermission.getPermission());
        }



        return assignedPermissions;
    }

    /**
     * 根据pid获取角色的权限
 * @param pid
     * @return java.util.List<com.barbeque.mall.bean.system.MarketRoleOptions>
     * @author 苏东宇
     * @since 2022/07/19 14:06
     */
    @Override
    public List<MarketRoleOptions> getMarketRoleOptionsByPid(Integer pid) {
        List<MarketRoleOptions> marketRoleOptions = marketRoleOptionsMapper.selectRoleOptionsByPid(pid);
        // //将MarketRoleOptions转化为FirstClass
        // for (MarketRoleOptions marketRoleOption : marketRoleOptions) {
        //     FirstClass firstClass = new FirstClass();
        //     BeanUtils.copyProperties(marketRoleOption,);
        // }

        return marketRoleOptions;
    }

    
    /**
     * 根据roleId删除所有的角色权限关系
 * @param roleId 
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 15:01 
     */
    @Override
    public int deleteRolePermissionByRoleId(Integer roleId) {
        MarketPermissionExample marketPermissionExample = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = marketPermissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);

        int i = marketPermissionMapper.deleteByExample(marketPermissionExample);

        return i;
    }

    /**
     * 往permission中插入数据
 * @param marketPermissionBO 
     * @return int
     * @author 苏东宇
     * @since 2022/07/19 15:07 
     */
    @Override
    public int createRolePermission(MarketPermissionBO marketPermissionBO) {
        List<String> permissions = marketPermissionBO.getPermissions();

        int num = 0;
        Integer roleId = marketPermissionBO.getRoleId();

        for (String permission : permissions) {
            MarketPermission marketPermission = new MarketPermission();
            marketPermission.setRoleId(roleId);
            marketPermission.setPermission(permission);
            marketPermission.setAddTime(new Date(System.currentTimeMillis()));
            marketPermission.setUpdateTime(new Date(System.currentTimeMillis()));
            marketPermission.setDeleted(false);
            int insert = marketPermissionMapper.insert(marketPermission);
            num += insert;
        }

        return num;
    }
}
